'''
Author: linxuan
Date: 2022-07-15 11:36:39
Description: file content
FilePath: /stealer/src/hdmeta.py
'''
import time
import requests
import json
from upload import upload
from hdmeta_upload import generate_hdmeta_params


def get_collection(page: int) -> dict:
    headers = {
        'authority': 'api.hdmeta.art',
        'accept': 'application/json',
        'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'authorization': 'undefined',
        'origin': 'https://www.hdmeta.art',
        'referer': 'https://www.hdmeta.art/',
        'sec-ch-ua': '" Not;A Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Linux"',
        'sec-fetch-dest': 'empty',
        'sec-fetch-mode': 'cors',
        'sec-fetch-site': 'same-site',
        'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.66 Safari/537.36',
    }
    params = {
        'page': '1',
    }

    response = requests.get(f'https://api.hdmeta.art/shop/collectible?page={page}', headers=headers)
    return response.json()


def get_detail(id: str) -> dict:
    headers = {
        'authority': 'api.hdmeta.art',
        'accept': 'application/json',
        'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'authorization': 'undefined',
        'origin': 'https://www.hdmeta.art',
        'referer': 'https://www.hdmeta.art/',
        'sec-ch-ua': '" Not;A Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Linux"',
        'sec-fetch-dest': 'empty',
        'sec-fetch-mode': 'cors',
        'sec-fetch-site': 'same-site',
        'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.66 Safari/537.36',
    }

    response = requests.get(f'https://api.hdmeta.art/shop/collectible/{id}', headers=headers)
    return response.json()


def main():
    '''
        遍历爬去网站数据，并存储到json文件里以供上传
    '''

    for page in range(25, 391):
        try:
            data = get_collection(page)
            for item in data['data']['data']:
                try:
                    detail = get_detail(item['id'])['data']
                    # with open(f'/resources/hdmeta/{item["id"]}.json'[1:], 'w') as f:
                    #     json.dump(detail, f, ensure_ascii=False)
                    params = generate_hdmeta_params(detail)
                    ok, err = upload(params)
                    if ok != 200:
                        print(f" - detail {item['id']} upload faild:", err)
                except Exception as e:
                    print(f" - detail {item['id']} get faild:", e.args[0])
                time.sleep(0.5)
        except Exception as e:
            print(f"---- page {page} faild:", e.args[0])
        print(f"---- page {page} done")


if __name__ == '__main__':
    main()
    print('over')
